﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Power.BLL;
using JN.Utility;

namespace LogManager.WinUI
{
    public partial class QueryAlarm : DevExpress.XtraEditors.XtraForm
    {
        #region property
        private Sys_alarmBLL alarmBLL = new Sys_alarmBLL();
        #endregion
        public QueryAlarm()
        {
            InitializeComponent();
        }

        private void QueryAlarm_Load(object sender, EventArgs e)
        {
            //initalize uI
            InitalizeUI();
            //load data
            using (new DevExpress.Utils.WaitDialogForm("正在加载数据...", "Please Wait..."))
            {
                LoadData();
            }
        }
        private void InitalizeUI()
        {
            try
            {
                //date time
                date1.DateTime = DateTime.Now.AddDays(-1);
                date2.DateTime = DateTime.Now.AddDays(1);

                //type
                cmbType.Properties.Items.Clear();
                cmbType.Properties.Items.Add("ALL");
                foreach (string types in Enum.GetNames(typeof(AlarmType)))
                {
                    cmbType.Properties.Items.Add(types);
                }
                cmbType.SelectedIndex = 0;

                //get station
                Station_infoBLL stBLL = new Station_infoBLL();
                DataTable dtRows = stBLL.GetAllList().Tables[0];
                cmbStationId.Properties.DataSource = dtRows;
                cmbStationId.Properties.DisplayMember = "station_name";
                cmbStationId.Properties.ValueMember = "station_id";

            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        private void LoadData()
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append(" 1>0");
                if (date1.Text != "")
                {
                    sql.Append(" and alarm_time >='" + date1.Text + "' ");
                }
                if (date2.Text != "")
                {
                    sql.Append(" and alarm_time <='" + date2.Text + "' ");
                }
                if (cmbType.Text != "ALL")
                {
                    sql.Append(" and alarm_type ='" + cmbType.Text + "' ");
                }
                if (cmbStationId.Text != "")
                {
                    sql.Append(" and station_id ='" + cmbStationId.EditValue + "' ");
                }

                DataTable datRow = alarmBLL.GetList(sql.ToString()).Tables[0];

                dgvData.DataSource = datRow;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (new DevExpress.Utils.WaitDialogForm("正在加载数据...", "Please Wait..."))
            {
                LoadData();
            }
        }
    }
}